package com.woniu.repaircenter.service.controller;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.annotation.RequireRole;
import com.woniu.repaircenter.models.dto.RepairMaterialDTO;
import com.woniu.repaircenter.models.form.AddRepairMaterialForm;
import com.woniu.repaircenter.models.param.AddRepairMaterialParam;
import com.woniu.repaircenter.service.service.RepairMaterialService;
import com.woniu.utils.JwtUtil;
import com.woniu.utils.Result;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 维修材料表 前端控制器
 * </p>
 *
 * @author 吕雨泽
 * @since 2024年12月06日
 */
@RestController
@RequestMapping("/repairMaterial")
@Api(tags = "维修材料管理")
public class RepairMaterialController {

    @Autowired
    private RepairMaterialService repairMaterialService;

    /**
     * 根据材料编号查询材料使用情况
     * @param materialCode 材料编号
     * @return 材料使用情况
     * @throws Exception 异常
     */
    @GetMapping("/selectAll")
    @ApiOperation("查询所有材料")
    public Result selectAll(
            @RequestParam("materialCode") @ApiParam(name = "materialCode", value = "材料编号", required = true) String materialCode) throws Exception {
        List<RepairMaterialDTO> list = repairMaterialService.selectAll(materialCode);
        return Result.getResult(List.class).setData(list).setMsg("查询成功").setCode(200);
    }

    /**
     * 申请维修材料
     * @param armf  维修材料
     * @param token 令牌
     * @return  申请结果
     * @throws Exception  异常
     */
    @PostMapping("/applyRepairMaterial")
    @ApiOperation("申请维修材料")
    public Result applyRepairMaterial(
            @Valid @RequestBody @ApiParam(name = "applyRepairMaterial", value = "申请维修材料", required = true) List<AddRepairMaterialForm> armf,
            @RequestHeader("token")String token) throws Exception {
        Claims claims = JwtUtil.parseToken(token);
        String userId = claims.get("userId").toString();
        Integer integer = repairMaterialService.addRepairMaterial(BeanUtil.copyToList(armf, AddRepairMaterialParam.class),userId);
        return Result.getResult(Integer.class).setData(integer).setMsg("申请成功").setCode(200);
    }

    /**
     *  更新维修材料
     * @param armf 维修材料
     * @param token  令牌
     * @return 更新结果
     * @throws Exception 异常
     */
    @PutMapping("/updateRepairMaterial")
    @RequireRole("维修员")
    @ApiOperation("更新维修材料")
    public Result updateRepairMaterial(
            @Valid@RequestBody @ApiParam(name = "updateRepairMaterial", value = "更新维修材料", required = true)  List<AddRepairMaterialForm> armf,
            @RequestHeader("token")String token) throws Exception {
        Claims claims = JwtUtil.parseToken(token);
        String userId = claims.get("userId").toString();
        Integer integer = repairMaterialService.updateRepairMaterial(BeanUtil.copyToList(armf, AddRepairMaterialParam.class),userId);
        return Result.getResult(Integer.class).setData(integer).setMsg("更新成功").setCode(200);
    }

    /**
     * 根据维修工单号查询维修材料
     * @param repairNum 维修工单号
     * @return 维修材料
     * @throws Exception 异常
     */
    @GetMapping("/selectByWorkOrderNumber")
    @ApiOperation("根据工单号查询维修材料")
    public Result selectByWorkOrderNumber(
            @RequestParam("repairNum") @ApiParam(name = "repairNum", value = "工单号", required = true) String repairNum) throws Exception {
        List<RepairMaterialDTO> list = repairMaterialService.selectByRepairNum(repairNum);
        return Result.getResult(List.class).setData(list).setMsg("查询成功").setCode(200);
    }

}

